import { _decorator, Component, Node, tween, Vec3, Animation, AnimationClip, resources, AnimationState, systemEvent, SystemEvent, game} from 'cc';
const { ccclass, property } = _decorator;

@ccclass('JumpEffect')
export class JumpEffect extends Component {
    @property(Node)
    targetNode: Node = null!;  // 目标节点

    @property(Node)
    strutBut: Node = null!;  //开始游戏按钮

    @property(Node)
    sortBut: Node = null!;  //排行榜按钮

    @property
    duration: number = 1;  // 动画的持续时间

    @property
    height: number = 500;  // 跳跃的初始高度


    @property(Animation)
    startBtnAni: Animation = null

    @property(Animation)
    sortBtnAni: Animation = null

    @property(Animation)
    pageShow: Animation = null


    @property(Node)
    lifeNode: Node = null



    start() {
        this.jump();

        // 给第一个按钮添加点击事件
        this.strutBut.on('click', this.strutButClick, this);

        // 给第二个按钮添加点击事件
        this.sortBut.on('click', this.sortButClick, this);
        
        game.on("enterLevel",this.enterAni,this)
        
    }

    jump() {

        tween(this.targetNode).
            set({ angle: -90, position: new Vec3(-324, 620, 0) }).
            to(2, { angle: -30, position: new Vec3(-324, -50, 0) }, {
                easing: 'bounceOut',
            }).
            start()
    }

    strutButClick() {

        this.strutBut.getComponent(Animation).on(Animation.EventType.FINISHED, this.ainBreak, this);
        // 播放动画
        this.strutBut.getComponent(Animation).getState("outButAni").wrapMode = AnimationClip.WrapMode.Reverse;
        this.sortBut.getComponent(Animation).getState("outButAni-001").wrapMode = AnimationClip.WrapMode.Reverse;
        this.strutBut.getComponent(Animation).play()
        
        this.sortBut.getComponent(Animation).play()
        console.log('点击了')

    }

    //开始按钮动画回调
    ainBreak(type: Animation.EventType, state: AnimationState){
        console.log('回调执行')
        this.strutBut.active = false;
        this.sortBut.active = false;
        
        this.levelPage()
    }

    tweenUp() {
        tween(this.targetNode).
            to(2, { angle: -90, position: new Vec3(-324, 620, 0), }, {
                easing: 'quintInOut',
            }).start();
    }


    levelPage() {
        console.log('播放了')
        this.pageShow.node.active = true
        this.pageShow.node.getComponent(Animation).getState("showpage").wrapMode = AnimationClip.WrapMode.Normal;
        this.pageShow.node.getComponent(Animation).play()
        this.lifeNode.getChildByName('levelBgpng').getComponent(Animation).getState('topAni').wrapMode = AnimationClip.WrapMode.Normal
        this.pageShow.on(Animation.EventType.FINISHED, ()=>{
            console.log("页面动画后")
            
            this.lifeNode.getChildByName('levelBgpng').getComponent(Animation).play();
        }, this);

        // this.pageShow.node.active = false

    }


    //level等级页面的返回
    exitHome() {
        console.log("退回主页")
        this.strutBut.getComponent(Animation).off(Animation.EventType.FINISHED, this.ainBreak, this);
        this.pageShow.node.getComponent(Animation).getState('showpage').wrapMode = AnimationClip.WrapMode.Reverse
        this.lifeNode.getChildByName('levelBgpng').getComponent(Animation).getState('topAni').wrapMode = AnimationClip.WrapMode.Reverse
        this.pageShow.node.getComponent(Animation).play();
        this.strutBut.active = true;
        this.sortBut.active = true;

        this.strutBut.getComponent(Animation).getState("outButAni").wrapMode = AnimationClip.WrapMode.Normal;
        this.sortBut.getComponent(Animation).getState("outButAni-001").wrapMode = AnimationClip.WrapMode.Normal;
        this.strutBut.getComponent(Animation).play()
        this.sortBut.getComponent(Animation).play()

        
        // this.strutBut.getComponent(Animation).play()
        // this.sortBut.getComponent(Animation).play()

        // this.pageShow.getComponent(Animation).on('finished', this.cell(), this);

    }

    enterAni(){
        console.log("显示退出动画")
    }

    cell() {
        // this.strutBut.active = true;
        // this.sortBut.active = true;
        // this.strutBut.getComponent(Animation).play()
        // this.sortBut.getComponent(Animation).play()
        // this.pageShow.getComponent(Animation).on('finished', this.cell(), this);
    }

    //排行榜按钮点击
    sortButClick() {

    }

    onDestroy() {
        game.off("enterLevel",this.enterAni,this)
    }





}
